package algebra.op;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import option.QueryKeywords;

import utils.Display;

import kb.DescriptionLogicsKB;

import entities.assertion.ConceptAssertion;
import entities.role.Role;
import algebra.query.pd.ProjDesc;
import algebra.visitors.ProjectionOPEvaluator;

public final class ProjectionOP extends AlgebraicOP {
	private final ProjDesc pd;
	private final DescriptionLogicsKB kb;
	private final AlgebraicOP sub;

	// public ProjectionOP(ProjectionDescription pd, AlgebraicOP sub){
	// this.pd = pd;
	// this.sub = sub;
	// this.kb = null;
	// }

	public ProjectionOP(ProjDesc pd, AlgebraicOP sub,
			DescriptionLogicsKB kb) {
		this.pd = pd;
		this.sub = sub;
		this.kb = kb;
	}

	public List<ConceptAssertion> acceptVisitor(ProjectionOPEvaluator eval) {
		return eval.visit(this);
	}

	public AlgebraicOP getSubquery() {
		return this.sub;
	}

	public ProjDesc getPrjDesc() {
		return this.pd;
	}

	@Override
	public boolean isPure() {
		return sub.isPure();
	}

	@Override
	public String toString() {
		return Display.QPROJECTIONUNICODE + "["
				+ (this.getPlanEvalKB() == null ? " ]" : "K]") + "_"
				+ this.pd.toString() + "_(" + this.sub.toString() + ")";
	}

	@Override
	public String toSerializedString() {
		return " (" + QueryKeywords.QPROJ + "["
				+ (this.getPlanEvalKB() == null ? " ]" : "K]") + " "
				+ this.sub.toSerializedString() + " "
				+ this.pd.toSerializedString() + ")";
	}

	@Override
	public DescriptionLogicsKB getPlanEvalKB() {
		return this.kb;
	}

	// public void setPlanEvalKB( DescriptionLogicsKB kb){
	// this.kb = kb;
	// }

	@Override
	public Set<Role> extractFeatures() {
		Set<Role> fs = new HashSet<Role>();
		fs.addAll(this.pd.extractFeatures());
		fs.addAll(this.sub.extractFeatures());
		return fs;
	}

	@Override
	public Set<String> extractConstants() {
		Set<String> ks = new HashSet<String>();
		ks.addAll(this.sub.extractConstants());
		return ks;
	}

	@Override
	public boolean isLeaf() {
		return false;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((kb == null) ? 0 : kb.hashCode());
		result = prime * result + ((pd == null) ? 0 : pd.hashCode());
		result = prime * result + ((sub == null) ? 0 : sub.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof ProjectionOP))
			return false;
		ProjectionOP other = (ProjectionOP) obj;
		if (kb == null) {
			if (other.kb != null)
				return false;
		} else if (!kb.equals(other.kb))
			return false;
		if (pd == null) {
			if (other.pd != null)
				return false;
		} else if (!pd.equals(other.pd))
			return false;
		if (sub == null) {
			if (other.sub != null)
				return false;
		} else if (!sub.equals(other.sub))
			return false;
		return true;
	}

	@Override
	public AlgebraicOP getKFreeVersion() {
		return new ProjectionOP(this.pd, this.sub, null);
	}

}
